import request from '@/utils/request'
import type { ApiResponse, PageResponse } from '@/types/api'

// 获取监控统计数据
export const getMonitorStatistics = (): Promise<{
  todayCount: number
  todayTrend: number
  successCount: number
  successTrend: number
  collisionCount: number
  collisionTrend: number
  errorCount: number
  errorTrend: number
}> => {
  return request.get('/api/monitor/statistics')
}

// 获取趋势数据
export const getTrendData = (timeRange: string): Promise<{
  xAxis: string[]
  inputData: number[]
  successData: number[]
  errorData: number[]
}> => {
  return request.get(`/api/monitor/trend/${timeRange}`)
}

// 获取渠道统计
export const getChannelStatistics = (dateRange: string): Promise<Array<{
  channelName: string
  count: number
}>> => {
  return request.get('/api/monitor/channel-statistics', {
    params: { dateRange }
  })
}

// 获取监控数据
export const getMonitorData = (params: {
  pageNum: number
  pageSize: number
  channelId?: string
  status?: string
}): Promise<PageResponse<any>> => {
  return request.get('/api/monitor/data', { params })
}

// 获取实时数据
export const getRealTimeData = (): Promise<{
  totalInput: number
  totalSuccess: number
  totalError: number
  totalCollision: number
  todayInput: number
  todaySuccess: number
  todayError: number
  todayCollision: number
  currentHourInput: number
  currentHourSuccess: number
  currentHourError: number
  currentHourCollision: number
}> => {
  return request.get('/api/monitor/realtime')
}

// 获取异常统计
export const getErrorStatistics = (params: {
  startDate: string
  endDate: string
  channelId?: string
}): Promise<Array<{
  errorType: string
  count: number
  percentage: number
}>> => {
  return request.get('/api/monitor/error-statistics', { params })
}

// 获取性能指标
export const getPerformanceMetrics = (): Promise<{
  avgProcessingTime: number
  maxProcessingTime: number
  minProcessingTime: number
  throughput: number
  errorRate: number
  collisionRate: number
}> => {
  return request.get('/api/monitor/performance')
}

// 获取告警配置
export const getAlertConfig = (): Promise<Array<{
  id: number
  name: string
  type: string
  threshold: number
  enabled: boolean
  notifyChannels: string[]
}>> => {
  return request.get('/api/monitor/alert-config')
}

// 更新告警配置
export const updateAlertConfig = (id: number, config: any): Promise<ApiResponse> => {
  return request.put(`/api/monitor/alert-config/${id}`, config)
}

// 获取告警记录
export const getAlertRecords = (params: {
  pageNum: number
  pageSize: number
  startDate?: string
  endDate?: string
  status?: string
}): Promise<PageResponse<any>> => {
  return request.get('/api/monitor/alert-records', { params })
}

// 处理告警
export const handleAlert = (id: number, action: string): Promise<ApiResponse> => {
  return request.post(`/api/monitor/alert/${id}/handle`, { action })
}